#coding=utf-8

#產生lonote的各種程式路徑
#發覺在lonote的程式碼中，有一堆要解算路徑的地方，受不了了！能夠獨立出來的全部都獨立放在這裡吧！

import os, sys, shutil, filecmp

class __Fix:
    '''程式啟動之後就不會變動的路徑'''
    @property
    def progDir(self):
        '''執行檔資料夾'''
        try:
            return self.__progDir
        except AttributeError:
            self.__progDir = os.path.abspath(os.path.dirname(sys.argv[0]))
            return self.__progDir

    @property
    def cfgDir(self):
        '''可存取的設定檔資料夾路徑'''
        try:
            return self.__lonoteConfigPath
        except AttributeError:
            if os.access(self.progDir, os.W_OK):
                self.__lonoteConfigPath = self.progDir
            else:
                configPath = os.path.join(os.getenv('HOME'), '.config')
                self.__lonoteConfigPath = os.path.join(configPath, 'lonote')
                if not os.path.isdir(configPath):
                    os.mkdir(configPath)
                if not os.path.isdir(self.__lonoteConfigPath):
                    os.mkdir(self.__lonoteConfigPath)
            return self.__lonoteConfigPath

    @property
    def iconDir(self):
        '''icon資料夾路徑'''
        try:
            return self.__iconDir
        except AttributeError:
            self.__iconDir = os.path.join(self.progDir, 'icons')
            #如果路徑是?/bin/，退出bin進入?/share/lonote/icons
            if not os.path.isdir(self.__iconDir):
                self.__iconDir = os.path.join(
                        os.path.dirname(self.progDir),
                        'share',
                        'lonote',
                        'icons')
            return self.__iconDir

    @property
    def templateDir(self):
        '''取得實際template資料夾路徑，如果路徑無效的話，用defaut寫入'''
        try:
            return self.__templateDir
        except AttributeError:
            self.__templateDir = os.path.join(self.cfgDir, 'template')
            if not os.path.isdir(self.__templateDir):
                shutil.copytree(self.__defaultTemplateDir(), self.__templateDir)
            return self.__templateDir

    @property
    def cssDir(self):
        '''取得實際要用的CSS目錄路徑'''
        try:
            return self.__cssDir
        except AttributeError:
            self.__cssDir = os.path.join(self.cfgDir, 'css')
            #如果不存在拷貝全部
            if not os.path.isdir(self.__cssDir):
                shutil.copytree(self.__defaultCSSDir(), self.__cssDir)
            else: #如果存在，選擇default有的但cssDir沒有的拷貝過去
                defCSSDir = self.__defaultCSSDir()
                for name in filecmp.dircmp(defCSSDir, self.__cssDir).left_only:
                    path = os.path.join(defCSSDir, name)
                    if os.path.isfile(path):
                        shutil.copy(path, os.path.join(self.__cssDir, name))
            return self.__cssDir

    @property
    def nav(self):
        '''取得實際Nav模版檔案路徑'''
        try:
            return self.__nav
        except AttributeError:
            self.__nav = self.__defaultNav()
            return self.__nav

    @property
    def localeDir(self):
        '''取得locale的目錄'''
        try:
            return self.__localeDir
        except AttributeError:
            self.__localeDir = os.path.join(self.progDir, 'locale')
            #如果路徑是?/bin/，退出bin進入?/share/locale
            if not os.path.isdir(self.__localeDir):
                self.__localeDir = os.path.join(
                        os.path.dirname(self.progDir),
                        'share',
                        'locale')
            return self.__localeDir

    @property
    def language(self):
        '''語系設定檔'''
        try:
            return self.__language
        except AttributeError:
            self.__language = os.path.join(self.cfgDir, 'language')
            return self.__language

    @property
    def cfg(self):
        '''程式設定檔'''
        try:
            return self.__cfg
        except AttributeError:
            self.__cfg = os.path.join(self.cfgDir, 'lonoterc')
            return self.__cfg

    @property
    def exampleDir(self):
        '''取得範例的總目錄'''
        try:
            return self.__exampleDir
        except AttributeError:
            self.__exampleDir = os.path.join(self.progDir, 'example')
            #如果路徑是?/bin/，退出bin進入?/share/lonote/example
            if not os.path.isdir(self.__exampleDir):
                self.__exampleDir = os.path.join(
                        os.path.dirname(self.progDir),
                        'share',
                        'lonote',
                        'example')
            return self.__exampleDir

    @property
    def jsDir(self):
        '''javaScript 的資料夾'''
        try:
            return self.__jsDir
        except AttributeError:
            self.__jsDir = os.path.join(self.progDir, 'javascript')
            #如果路徑是?/bin/，退出bin進入?/share/lonote/example
            if not os.path.isdir(self.__jsDir):
                self.__jsDir = os.path.join(
                        os.path.dirname(self.progDir),
                        'share',
                        'lonote',
                        'javascript')
            return self.__jsDir

    def __defaultTemplateDir(self):
        '''取得系統的template資料夾路徑'''
        defaultTemplateDir = os.path.join(
                self.progDir, 'default', 'template')
        #如果路徑是?/bin/，退出bin進入?/share/lonote/default/template
        if not os.path.isdir(defaultTemplateDir):
            defaultTemplateDir = os.path.join(
                    os.path.dirname(self.progDir),
                    'share',
                    'lonote',
                    'default',
                    'template')
        return defaultTemplateDir

    def __defaultCSSDir(self):
        '''取得系統預設的CSS目錄路徑'''
        defaultCSSDir = os.path.join(self.progDir, 'default', 'css')
        #如果路徑是?/bin/，退出bin進入?/share/lonote/default
        if not os.path.isdir(defaultCSSDir):
            defaultCSSDir = os.path.join(
                    os.path.dirname(self.progDir),
                    'share',
                    'lonote',
                    'default',
                    'css')
        return defaultCSSDir

    def __defaultNav(self):
        '''取得系統預設的Nav模板檔案路徑'''
        defaultNav = os.path.join(self.progDir, 'default', 'nav.html')
        #如果路徑是?/bin/，退出bin進入?/share/lonote/default
        if not os.path.isfile(defaultNav):
            defaultNav = os.path.join(os.path.dirname(self.progDir),
                    'share',
                    'lonote',
                    'default',
                    'nav.html')
        return defaultNav

fix = __Fix()

class icons:
    #Editor
    bgcol = os.path.join(fix.iconDir, 'bgcol.png')
    bold = os.path.join(fix.iconDir, 'bold.png')
    bullist = os.path.join(fix.iconDir, 'bullist.png')
    center = os.path.join(fix.iconDir, 'center.png')
    forecol = os.path.join(fix.iconDir, 'forecol.png')
    indate = os.path.join(fix.iconDir, 'indate.png')
    indent = os.path.join(fix.iconDir, 'indent.png')
    inhtml = os.path.join(fix.iconDir, 'inhtml.png')
    inimg = os.path.join(fix.iconDir, 'inimg.png')
    intemplate = os.path.join(fix.iconDir, 'intemplate.png')
    intime = os.path.join(fix.iconDir, 'intime.png')
    italic = os.path.join(fix.iconDir, 'italic.png')
    left = os.path.join(fix.iconDir, 'left.png')
    link = os.path.join(fix.iconDir, 'link.png')
    noformat = os.path.join(fix.iconDir, 'noformat.png')
    right = os.path.join(fix.iconDir, 'right.png')
    ordlist = os.path.join(fix.iconDir, 'ordlist.png')
    outdent = os.path.join(fix.iconDir, 'outdent.png')
    strike = os.path.join(fix.iconDir, 'strike.png')
    sub = os.path.join(fix.iconDir, 'sub.png')
    sup = os.path.join(fix.iconDir, 'sup.png')
    underline = os.path.join(fix.iconDir, 'underline.png')
    unlink = os.path.join(fix.iconDir, 'unlink.png')
    #Editor Context Menu
    editCopy = os.path.join(fix.iconDir, 'edit-copy.png')
    editCut = os.path.join(fix.iconDir, 'edit-cut.png')
    editPaste = os.path.join(fix.iconDir, 'edit-paste.png')
    
    #Tree Context Menu
    treedelnote = os.path.join(fix.iconDir, 'delnote22.png')
    treenewnote = os.path.join(fix.iconDir, 'newnote22.png')
    treenewsubnote = os.path.join(fix.iconDir, 'newsubnote22.png')
    
    #Attachments
    inatt = os.path.join(fix.iconDir, 'inatt.png')
    
    #Find
    before = os.path.join(fix.iconDir, 'before.png')
    close = os.path.join(fix.iconDir, 'close.png')
    yes = os.path.join(fix.iconDir, 'yes.png')
    
    #mainApp
    delnote = os.path.join(fix.iconDir, 'delnote.png')
    newnote = os.path.join(fix.iconDir, 'newnote.png')
    newsubnote = os.path.join(fix.iconDir, 'newsubnote.png')
    
    loadnotebook = os.path.join(fix.iconDir, 'loadnotebook.png')
    notebook = os.path.join(fix.iconDir, 'notebook.png')
    newnotebook = os.path.join(fix.iconDir, 'newnotebook.png')
    export = os.path.join(fix.iconDir, 'export.png')
    exportnote = os.path.join(fix.iconDir, 'export.png')
    importnote = os.path.join(fix.iconDir, 'import.png')
   
    lonote = os.path.join(fix.iconDir, 'lonote.png')
    about = os.path.join(fix.iconDir, 'about.png')
    aboutbutton = os.path.join(fix.iconDir, 'aboutbutton.png')
    questiondialog = os.path.join(fix.iconDir, 'questiondialog.png')
    error = os.path.join(fix.iconDir, 'error.png')
    cfgdialog = os.path.join(fix.iconDir, 'cfg.png')
    khappy = os.path.join(fix.iconDir, 'khappy.png')
    kworking = os.path.join(fix.iconDir, 'kworking.png')
    fairy = os.path.join(fix.iconDir, 'fairy.png')


#範例資料夾
exampleDir = fix.exampleDir

class cal:
    '''程式啟動後會隨時變動，需要給值計算後才能獲知的路徑'''
    def bookdb(bookdir):
        '''透過bookdir取得booknote.db的位置'''
        return os.path.join(bookdir, 'notebook.db')
    def exportdir(bookdir):
        '''透過bookdir取得匯出路徑'''
        return os.path.join(bookdir, 'export')
    def backupdir(bookdir):
        '''透過bookdir取得備份路徑'''
        return os.path.join(bookdir, 'backup')
    def booklock(bookdir):
        '''透過bookdir取得booknote.db的位置'''
        return os.path.join(bookdir, 'FILELOCK.lock')
    def bhistory(bookdir):
        '''透過bookdir取得booknote的位置'''
        return os.path.join(bookdir, 'browsinghistory')
    def exportnav(bookdir):
        '''透過bookdir取得book的nav模版'''
        #筆記本nav放在筆記本路徑下
        b = os.path.join(bookdir, 'nav.html')
        if os.path.isfile(b):
            return b
        return fix.nav
#    def exportframe(bookdir):
#        '''透過bookdir取得book的frame模版'''
#        #筆記本frame放在筆記本路徑下
#        b = os.path.join(bookdir, 'frame.html')
#        if os.path.isfile(b): return b
#        return lonoteFrame
    def notedir(bookdir, id):
        '''透過bookdir與id取得單一筆記的個體路徑'''
        return os.path.join(bookdir, 'data', str(id))
    def noteemb(bookdir, id):
        '''透過bookdir與id取得圖檔的路徑'''
        return os.path.join(bookdir, 'data', str(id), 'embedded')
    def noteatt(bookdir, id):
        '''透過bookdir與id取得附件的路徑'''
        return os.path.join(bookdir, 'data', str(id), 'attached')
    def notecss(bookdir, id, defaultCSS = 'lonote.css'):
        '''透過bookdir與id取得css的路徑，回傳css檔案'''
        #頁面專用css放在notedir中
        n = os.path.join(cal.notedir(bookdir, id), 'note.css')
        if os.path.isfile(n): return n
        #筆記本css放在筆記本路徑下
        b = os.path.join(bookdir, 'notebook.css')
        if os.path.isfile(b): return b
        #全域css放在cssDir底下
        return os.path.join(fix.cssDir, defaultCSS)
